<?php
require_once '../../../wp-blog-header.php';
require_once(ABSPATH . WPINC . '/registration.php');
header('HTTP/1.0 200 OK');

$is_ajax_valid = check_ajax_referer('newsletter_ajax','ajax_nonce');

if($is_ajax_valid == 1){
	
	$email = $_POST['newsletter_email'];
	$city = explode('-',$_POST['newsletter_city']);
	$city_id = $city[0];
	$city_name = strtolower($city[1]);
	$value = $_POST['newsletter_signup'];

	$result = buzzr_subscribe_newsletter($email,$city_id);
	if($result == 'success'){
		
		$pref_link = BUZZR_URL.'/emailpref?p='.encrypt(sanitize_title($email),'1b2u3z4z5r6');
		$msg = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
    
      <!-- Header -->
      <div style="background-color: #2589BC; width: 600px;">
        <div style="float: left; padding: 10px;">
          <a href="http://buzzr.in" target="_blank"><img style="border:none;" src="http://buzzr.in/wp-content/themes/buzzr_960/img/buzzrin-logo.png" alt="Buzzr.in" title="Buzzr.in" /></a>
        </div>
        <div style="float: right; margin-top: 80px;">
           <ul style="padding: 0; margin: 0;">
             <li style="float: left;list-style: none;"><a target="_blank" href="http://buzzr.in/deals" style="text-decoration: none;
background-color: #76BF26;
padding: 10px;
font: bold 15px Arial;
color: white;
margin-right: 5px;
-webkit-border-radius: 10px 10px 0px 0px;
-moz-border-radius: 10px 10px 0px 0px;
display: block;">Product Deals</a></li>
             <li style="float: left;list-style: none;"><a target="_blank" href="http://buzzr.in/localoffers" style="text-decoration: none;
background-color: #76BF26;
padding: 10px;
font: bold 15px Arial;
color: white;
margin-right: 5px;
-webkit-border-radius: 10px 10px 0px 0px;
-moz-border-radius: 10px 10px 0px 0px;
display: block;">Local Offers</a></li>
             <li style="float: left;list-style: none;"><a target="_blank" href="http://buzzr.in/blog" style="text-decoration: none;
background-color: #76BF26;
padding: 10px;
font: bold 15px Arial;
color: white;
margin-right: 5px;
-webkit-border-radius: 10px 10px 0px 0px;
-moz-border-radius: 10px 10px 0px 0px;
display: block;">Blog</a></li>
           </ul>
         </div>
         <div style="clear: both;"></div>
        <!-- Header End -->
        <!-- Main Content -->
        
        <div style="padding: 10px;background-color: white;font-family: calibri;line-height: 23px;"> 
          <div style="border: 3px solid #DDD; text-align: center; border-radius: 10px;">
          <div style="border-bottom: 1px solid #ddd;">
            <div style="margin-bottom: 10px;margin-left: 10px;margin-top: 10px;width: 550px;">
            
            <div style="text=align: center;">
            <h2>Thank You For Joining Us!</h2>
            We welcome you to Buzzr.in. Enjoy thousands of live deals all across India at<br /> incredibly low prices. Get regular email updates about extraordinary deals that <br /> save you big bucks at local restaurants, products, spas, and more.
            </div>
            
            <div style="margin-left: 10px;margin-top: 10px;text-align: center;">
             <a href="'.$pref_link.'">Click here to manage your preferences</a>  
            </div>
            
            </div>
            
          </div>
          <div>
              <div style="float: left; padding: 10px 20px;"><p style="font: 18px Arial;">Follow us on</p><a href="http://www.facebook.com/BuzzrIndia" target="_blank"><img style="border:none;" title="Buzzr on Facebook" alt="Facebook" src="http://buzzr.in/wp-content/themes/buzzr_960/img/fb.png" width="30px" height="30px"/></a><a href="http://www.youtube.com/buzzrindia" target="_blank"><img  style="border:none;" title="Buzzr on YouTube" alt="YouTube" src="http://buzzr.in/wp-content/themes/buzzr_960/img/youtube.png" width="30px" height="30px" /></a><a href="http://twitter.com/BuzzrIndia"><img style="border:none;" title="Buzzr on Twitter" alt="Twitter" src="http://buzzr.in/wp-content/themes/buzzr_960/img/twitter.png" width="30px" height="30px" /></a></div>
              <div style="float: left; padding: 10px 20px;">
                <div style="float: left;"><p style="font: 18px Arial; width: 71px;">Grab our Widget</p></div>
                <div style="float: left;"><a href="http://buzzr.in/getwidget" target="_blank"><img style="border:none;" title="Get Our Widget" alt="Get Our Widget" src="http://buzzr.in/wp-content/pfiles/images/widget.png" /></a></div>
                <div style="clear: both;"></div>
              </div>
              <div style="float: left; padding: 10px 20px;">
                <div style="float: left;"><p style="font: 18px Arial; width: 90px;">Download Buzzrs Mobile App</p></div>
                <div style="float: left;"><a href="http://buzzr.in/mobile" target="_blank"><img style="border:none;" title="iPhone App" alt="iPhone App" src="http://buzzr.in/wp-content/pfiles/images/iphoneapp.png" /></a></div>
                <div style="clear: both;"></div>
              </div>
               <div style="clear: both;"></div>
            </div>
          </div>
        </div>
      </div>
      
  </body>
</html>';
		
		$to = $email;
		$sub = 'Buzzr.in: Thank You for Subscribing';
		$headers = array("From: Buzzr.in <info@enthrallmedia.com>","Content-Type: text/html");
	    $h = implode("\r\n",$headers) . "\r\n";
	    wp_mail($to, $sub, $msg, $h);
	    
	    if($value == 'Sign Up' || $value == 'Go'){	    
	    	$message = 'Thank you for subscribing, <a href="'.$pref_link.'" target="_blank">Click here</a> to manage your preferences, check out deals from <a target="_blank" href="'.BUZZR_URL.'/dailydeals/'.$city_name.'">'.$city_name.'</a>';
	    	echo json_encode(array('code'=>'1000', 'message' => $message));
		}elseif($value == 'Subscribe'){ 
			$url = BUZZR_URL.'/dailydeals/'.$city_name.'/?success=1';
			echo json_encode(array('code'=>'1002', url=> $url));
			session_start();
			$_SESSION['message']= 'Thank you for subscribing, <a href="'.$pref_link.'" target="_blank">Click here</a> to manage your preferences';
			$_SESSION['email_id'] = $email;
		}	    	    
	}elseif($result == 'exists'){
		if($value == 'Sign Up' || $value == 'Go'){	    
	    	$message = '<b>You are already Subscribed</b>';
	    	echo json_encode(array('code'=>'1004', 'message' => $message));
		}else{
			$url = BUZZR_URL.'/dailydeals/'.$city_name.'/?notice=1';
			echo json_encode(array('code'=>'1003', url=> $url));
			session_start();
			$_SESSION['message']= '<b>You are already Subscribed</b>';
		}
	}
}else{
		echo json_encode(array('1005'=>'Page open since too long, please reload to subscribe to the newsletters!'));
		
}
?>